package com.rustic.server.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.bean.IPage;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.po.NationalityDictionary;
import com.rustic.server.service.NationalityDictionaryService;
import com.rustic.server.vo.list.NationalityDictionaryWebListVO;
import com.rustic.server.vo.NationalityDictionaryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[nationality_dictionary]对应的前台接口
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/api/nationalityDictionary")
@Api(tags = {"国籍字典接口"})
public class NationalityDictionaryController {

    @Autowired
    private NationalityDictionaryService nationalityDictionaryService;

    @ApiOperation(value = "国籍字典-列表")
    @GetMapping("/list")
    public Result<Page<NationalityDictionaryWebListVO>> list(IPage page) {
        LambdaQueryWrapper<NationalityDictionary> queryWrapper = new LambdaQueryWrapper<>();
        //FIXME 根据需要修改搜索条件
        Page<NationalityDictionary> pageData = nationalityDictionaryService.page(page.toPage(), queryWrapper);
        List<NationalityDictionary> nationalityDictionaryList = pageData.getRecords();
        if (CollUtil.isEmpty(nationalityDictionaryList)) {
            return Result.data(new Page<>());
        }
        List<NationalityDictionaryWebListVO> results = nationalityDictionaryService.toWebListVo(nationalityDictionaryList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "国籍字典-详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "国籍字典id"),
    })
    @GetMapping("/detail")
    public Result<NationalityDictionaryVO> detail(int id) {
        if (id <= 0) {
            return Result.error(NationalityDictionaryService.NATIONALITY_DICTIONARY_NOT_EXIST);
        }
        NationalityDictionary nationalityDictionary = nationalityDictionaryService.getById(id);
        if (nationalityDictionary == null) {
            return Result.error(NationalityDictionaryService.NATIONALITY_DICTIONARY_NOT_EXIST);
        }
        NationalityDictionaryVO nationalityDictionaryVO = new NationalityDictionaryVO(nationalityDictionary);
        return Result.data(nationalityDictionaryVO);
    }

}
